function showDiv(elementId) {
	var mask = document.getElementById('mask');
	mask.style.display = "";
	var element = document.getElementById(elementId);
	element.style.display = "";
}
function hiddenDiv(elementId) {
	var mask = document.getElementById('mask');
	mask.style.display = "none";
	var element = document.getElementById(elementId);
	element.style.display = "none";
}
function sign(id) {
	var merchandiseIds = document.getElementsByName('merchandiseId');
	for ( var i = 0; i < merchandiseIds.length; i++) {
		var merchandiseId = merchandiseIds.item(i);
		if (merchandiseId.value == id) {
			merchandiseId.checked = true;
			break;
		}
	}
}
function insertButton() {
	clearValue();
	showDiv('sus');
}
function updateButton() {
	var merchandiseIds = document.getElementsByName('merchandiseId');
	var id = 0;
	for ( var i = 0; i < merchandiseIds.length; i++) {
		var merchandiseId = merchandiseIds.item(i);
		if (merchandiseId.checked == true) {
			id = merchandiseId.value;
			break;
		}
	}
	update(id);
}
function deleteButton() {
	deleteMer();
}
function selectButton() {
	clearValue();
	showDiv('sus');
	document.getElementById("add").style.display = "none";
	document.getElementById("modify").style.display = "none";
	document.getElementById("find").style.display = "";
}
function update(id) {
	document.getElementById("add").style.display = "none";
	document.getElementById("modify").style.display = "";
	document.getElementById("find").style.display = "none";
	if (id != 0) {
		showDiv('sus');
	} else {
		alert("请选择仓库");
	}
	var mer = document.getElementById('' + id);
	var str = '';
	var merchandiseId = mer.getAttribute('merchandiseId');
	var merchandiseUnitId = mer.getAttribute('merchandiseUnitId');
	var merchandiseUnitName = mer.getAttribute('merchandiseUnitName');
	var merchandiseTypeId = mer.getAttribute('merchandiseTypeId');
	var merchandiseTypeName = mer.getAttribute('merchandiseTypeName');
	var merchandiseCid = mer.getAttribute('merchandiseCid');
	var merchandiseName = mer.getAttribute('merchandiseName');
	var merchandiseAmount = mer.getAttribute('merchandiseAmount');
	var merchandiseNorm = mer.getAttribute('merchandiseNorm');
	var merchandiseAlert = mer.getAttribute('merchandiseAlert');
	var merchandiseMemo = mer.getAttribute('merchandiseMemo');

	str = merchandiseId + '.' + merchandiseUnitId + '.' + merchandiseTypeId
			+ '.' + merchandiseCid + '.' + merchandiseName + '.'
			+ merchandiseAmount + '.' + merchandiseNorm + '.'
			+ merchandiseAlert + '.' + merchandiseMemo;
	var strArr = str.split('.');
	setValue(strArr);
}
function clearValue() {
	document.getElementById("add").style.display = "";
	document.getElementById("modify").style.display = "none";
	document.getElementById("find").style.display = "none";
	document.all.merchandiseId.value = "";
	document.all.merchandiseUnit.value = "";
	document.all.merchandiseType.value = "";
	document.all.merchandiseCid.value = "";
	document.all.merchandiseName.value = "";
	document.all.merchandiseAmount.value = "";
	document.all.merchandiseNorm.value = "";
	document.all.merchandiseAlert.value = "";
	document.all.merchandiseMemo.value = "";
}
function setValue(strArr) {
	$("#merchandiseId").val(strArr[0]);
	$("#merchandiseUnit").val(strArr[1]);
	$("#merchandiseType").val(strArr[2]);
	// document.all.merchandiseId.value = strArr[0];
	// document.all.merchandiseUnit.value = strArr[1];
	// document.all.merchandiseType.value = strArr[2];
	document.all.merchandiseCid.value = strArr[3];
	document.all.merchandiseName.value = strArr[4];
	document.all.merchandiseAmount.value = strArr[5];
	document.all.merchandiseNorm.value = strArr[6];
	document.all.merchandiseAlert.value = strArr[7];
	document.all.merchandiseMemo.value = strArr[8];
}
function callback(data) {
	if ("" != data.lenght) {
		hiddenDiv('sus');
	}
	var str = "";
	for ( var i = 0; i < data.length; i++) {
		// 动态的改变页面中div节点中的内容
		str = str + '<tr id="tr' + i + '" ondblclick="update('
				+ data[i].merchandiseId + ',\'tr' + i + '\');" onclick="sign('
				+ data[i].merchandiseId + ');">'
				+ '<td width="20" ><input id="' + data[i].merchandiseId
				+ '" type="radio" name="merchandiseId" value="'
				+ data[i].merchandiseId + '" merchandiseId="'
				+ data[i].merchandiseId + '" merchandiseUnitId="'
				+ data[i].merchandiseUnitId + '" merchandiseUnitName="'
				+ data[i].merchandiseUnitName + '" merchandiseTypeId="'
				+ data[i].merchandiseTypeId + '" merchandiseTypeName="'
				+ data[i].merchandiseTypeName + '" merchandiseCid="'
				+ data[i].merchandiseCid + '" merchandiseName="'
				+ data[i].merchandiseName + '" merchandiseAmount="'
				+ data[i].merchandiseAmount + '" merchandiseNorm="'
				+ data[i].merchandiseNorm + '" merchandiseAlert="'
				+ data[i].merchandiseAlert + '" merchandiseMemo="'
				+ data[i].merchandiseMemo + '" /></td>' + '<td width="100" >'
				+ data[i].merchandiseCid + '</td>' + '<td width="100" >'
				+ data[i].merchandiseName + '</td>' + '<td width="100" >'
				+ data[i].merchandiseTypeName + '</td>' + '<td width="120" >'
				+ data[i].merchandiseNorm + '</td>' + '<td width="50" >'
				+ data[i].merchandiseUnitName + '</td>' + '<td width="80" >'
				+ data[i].merchandiseAmount + '</td>' + '<td width="120" >'
				+ data[i].merchandiseMemo + '</td>' + '<td width="120" >'
				+ data[i].merchandiseAlert + '</td>' + '</tr>';
	}
	clearValue();
	$("#merchandiseList").html(str);
}
// 添加 员工 部分
function createMer() {
	var merchandiseId = escape(escape($("#merchandiseId").val()));
	var merchandiseCid = escape(escape($("#merchandiseCid").val()));
	var merchandiseName = escape(escape($("#merchandiseName").val()));
	var merchandiseUnit = escape(escape($("#merchandiseUnit").val()));
	var merchandiseType = escape(escape($("#merchandiseType").val()));
	var merchandiseNorm = escape(escape($("#merchandiseNorm").val()));
	var merchandiseAmount = escape(escape($("#merchandiseAmount").val()));
	var merchandiseAlert = escape(escape($("#merchandiseAlert").val()));
	var merchandiseMemo = escape(escape($("#merchandiseMemo").val()));
	var url = '/stock/pages/system/merchandise.do?method=createMer&d='
			+ new Date() + '&merchandiseCid=' + merchandiseCid
			+ '&merchandiseName=' + escape(escape(merchandiseName))
			+ '&merchandiseUnitId=' + merchandiseUnit + '&merchandiseTypeId='
			+ merchandiseType + '&merchandiseNorm=' + merchandiseNorm
			+ '&merchandiseAmount=' + merchandiseAmount + '&merchandiseAlert='
			+ merchandiseAlert + '&merchandiseMemo='
			+ escape(escape(merchandiseMemo));
	alert(url);
	$.getJSON(url, {}, callback);
}
// 修改 员工 部分
function updateMer(iid) {
	var merchandiseCid = escape(escape($("#merchandiseCid").val()));
	var merchandiseId = escape(escape($("#merchandiseId").val()));
	var merchandiseName = escape(escape($("#merchandiseName").val()));
	var merchandiseUnit = escape(escape($("#merchandiseUnit").val()));
	var merchandiseType = escape(escape($("#merchandiseType").val()));
	var merchandiseNorm = escape(escape($("#merchandiseNorm").val()));
	var merchandiseAmount = escape(escape($("#merchandiseAmount").val()));
	var merchandiseAlert = escape(escape($("#merchandiseAlert").val()));
	var merchandiseMemo = escape(escape($("#merchandiseMemo").val()));
	var url = '/stock/pages/system/merchandise.do?method=updateMer2&d='
			+ new Date() + '&merchandiseCid=' + merchandiseCid
			+ '&merchandiseId=' + merchandiseId + '&merchandiseName='
			+ escape(escape(merchandiseName)) + '&merchandiseUnitId='
			+ merchandiseUnit + '&merchandiseTypeId=' + merchandiseType
			+ '&merchandiseNorm=' + merchandiseNorm + '&merchandiseAmount='
			+ merchandiseAmount + '&merchandiseAlert=' + merchandiseAlert
			+ '&merchandiseMemo=' + escape(escape(merchandiseMemo));
	alert(url);
	$.getJSON(url, {}, callback);
}
// 删除 员工 部分
function deleteMer() {
	if (confirm("真的要删除吗？删除后将无法恢复！")) {
		var merchandiseIdBox = document.getElementsByName("merchandiseId");
		var merchandiseId = 0;
		for ( var i = 0; i < merchandiseIdBox.length; i++) {
			if (merchandiseIdBox[i].checked) {
				merchandiseId = merchandiseIdBox[i].value;
				break;
			}
		}
		var url = '/stock/pages/system/merchandise.do?method=deleteMer&d='
				+ new Date() + '&merchandiseId=' + merchandiseId;
		alert(url);
		$.getJSON(url, {}, callback);
	}
}
// 查找 员工 部分
function selectMer() {
	var merchandiseCid = document.getElementById('merchandiseCid').value;
	var merchandiseName = document.getElementById('merchandiseName').value;
	var merchandiseType = document.getElementById('merchandiseType').value;
	var merchandiseNorm = document.getElementById('merchandiseNorm').value;
	var url = '/stock/pages/system/merchandise.do?method=selectMer&d='
			+ new Date() + '&merchandiseCid=' + escape(escape(merchandiseCid))
			+ '&merchandiseName=' + escape(escape(merchandiseName))
			+ '&merchandiseTypeId=' + escape(escape(merchandiseType))
			+ '&merchandiseNorm=' + escape(escape(merchandiseNorm));
	$.getJSON(url, {}, callback);
}
function selectAllMer() {
	var url = '/stock/pages/system/merchandise.do?method=selectMer&d=' + new Date();
	$.getJSON(url, {}, callback);
}